import io
import os
import sys
from utils.core import CoreCaller
from utils import readDBinfo, log
import json

# 如果传输中文，则需要修改编码
sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding="utf-8")

remote_db_config = readDBinfo(os.path.dirname(__file__)+'/conf.ini')

core_caller = CoreCaller(remote_db_config)


def read_in():
    """
    读取nodejs发送的json对象，格式：

    {
        search_tags: "意大利夺冠".
        details: ["2020", "英格兰", "梅西"]
    }
    """

    lines = sys.stdin.readlines()
    # Since our input would only be having one line, parse our JSON data from that
    return json.loads(lines[0])


def cluster():
    tagInput = read_in()

    log.logger.info(tagInput)

    relevant_result = core_caller.search_tag(
        tagInput["search_tag"], tagInput["detail"])
    relevant_result = json.dumps(dict(relevant_result))
    print(relevant_result)


if __name__ == "__main__":
    log.logger.info("cluster.py BEGIN...")
    cluster()
    log.logger.info("cluster.py END...")
